<?php

namespace app\admin\model;

use think\Model;

use think\Db;
use think\Request;

class Port extends Model
{
    //港口城市的列表
    public function getports($res)
    {
        $page = $res['page'];
        $limit = $res['pageSize'];
        $data = $this->page($page, $limit)->select();
        foreach ($data as $datakey => $dataval) {
            $data[$datakey]['countryname'] = getinfo($dataval['country_id'], 'country')['name'];
            //编辑者
            $data[$datakey]['updatename'] = getinfo($dataval['update_id'], 'admin')['nickname'];
            $data[$datakey]['update_date'] = getTimeDate($dataval['update_at'], 12);//更新时间
            if ($dataval['type'] == 1) {
                $data[$datakey]['typename'] = lang("export_warehouse");
            } else {
                $data[$datakey]['typename'] = lang("warehouse");
            }
        }
        $count = $this->count();
        return array('count' => $count, 'data' => $data);
    }

    //港口的添加与编辑
    public function addeditport($data)
    {
        $ckdata = notEmpty(['country_id' => lang("country"), 'name' => lang("name")]);
        if ($ckdata['status']==2) {
            return $ckdata;
        }
        //添加港口城市
        $name = trim($data['name']);
        if ($data['act'] == 'portadd') {
            $info = $this->where('name', $name)->find();
            if ($info) {
                return array('msg' => $data['name'] . lang("already_exists"), 'status' => 3);
            }
            unset($data['id']);
            $data['create_id'] = $data['update_id'] = session('userid');
            $data['create_at'] = $data['update_at'] = time();
            $portid = $this->strict(false)->insertGetId($data);//港口表
            if ($portid) {
                return array('msg' => $name . lang("added_successfully"), 'status' => 1);
            } else {
                return array('msg' => $name . lang("add_failed"), 'status' => 0);
            }
        } elseif ($data['act'] == 'portedit') {
            unset($data['act']);
            $data['update_id'] = session('userid');
            $data['update_at'] = time();
            $portid = $this->where('id', $data['id'])->update($data);//港口表
            if ($portid !== false) {
                return array('msg' => $name . lang("edited_successfully"), 'status' => 1);
            } else {
                return array('msg' => $name . lang("edit_failed"), 'status' => 0);
            }
        }
    }

    //港口的删除
    public function delport($data)
    {
        $portname = getinfo($data['id'], 'port')['name'];
        $retport = $this->where('id', $data['id'])->delete();//港口表
        if ($retport) {
            return array('msg' => $portname . lang("cleared_successfully"), 'status' => 1);
        } else {
            return array('msg' => $portname . lang("clear_failed"), 'status' => 0);
        }
    }
}